package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.WmSensitiveDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2023-04-21
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {


    @Override
    public PageResponseResult findList(WmSensitiveDto wmSensitiveDto) {
        LambdaQueryWrapper<WmSensitive> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(wmSensitiveDto.getName()),WmSensitive::getSensitives,wmSensitiveDto.getName().trim());
        Page<WmSensitive> page =new Page<>(wmSensitiveDto.getPage(),wmSensitiveDto.getSize());
        page(page,queryWrapper);
        return new PageResponseResult(wmSensitiveDto.getPage(),wmSensitiveDto.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives, wmSensitive); // 构建条件
        WmSensitive one = this.getOne(queryWrapper);
        if (one != null) {
//            已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
//        实现新增的方法
        wmSensitive.setCreatedTime(new Date());
        this.save(wmSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delSensitive(Integer id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        updateById(wmSensitive);
        return ResponseResult.okResult();
    }
}
